package com.chen.org.service.impl;

import com.chen.basic.service.impl.BaseServiceImpl;
import com.chen.org.domain.Employee;
import com.chen.org.mapper.EmployeeMapper;
import com.chen.org.query.EmployeeQuery;
import com.chen.org.service.IEmployeeService;
import com.chen.permission.domain.Role;
import com.chen.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    // 注入mapper
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        Role role = employee.getRole();
        employeeMapper.insert(employee);
        if (Objects.nonNull(employee.getRole()) && Objects.nonNull(role.getId())) {
            employeeMapper.insertEmpRole(employee.getId(),role.getId());
        }
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
        Role role = employee.getRole();
        if (Objects.nonNull(employee.getRole()) && Objects.nonNull(role.getId())) {
            employeeMapper.updateEmpRole(employee.getId(),role.getId());
        }
    }

    @Override
    public Employee getByUsername(String username) {
        return employeeMapper.getByUsername(username);
    }
}
